package com.jltx.storage.downloader;

import android.content.Context;
import java.util.List;
import io.reactivex.Observable;
import okhttp3.OkHttpClient;

/**
 * @author jltxseo
 * Created by junlintianxia on 2019/12/17.
 */
public class StorageDownloader {

    public static void init(Context context) {
        Downloader.getInstance().init(context);
    }

    /**
     * 使用前必须初始化
     *
     * @param okHttpClient
     */
    public static void initHttpClient(OkHttpClient okHttpClient) {
        Downloader.getInstance().initHttpClient(okHttpClient);
    }


    public static void unSubscribe() {
        Downloader.getInstance().unSubscribe();
    }

    /**
     * 通过tag取消下载任务
     *
     * @param tag
     */
    public static void cancel(Object tag) {
        Downloader.getInstance().cancel(tag);
    }

    /**
     * 下载单个文件
     *
     * @param tag          标记下载任务的tag
     * @param url          要下载的文件的url链接
     * @param localPath    要下载的文件输出本地全路径
     * @param fileCallback 回调监听器
     */
    public static void download(final Object tag, final String url, final String localPath, final FileCallback<RequestResult> fileCallback) {
        Downloader.getInstance().download(tag, url, localPath, fileCallback);
    }

    /**
     * 多个视频批量下载
     *
     * @param tag
     * @param urlList
     * @param localPathList
     * @param multiFileCallback
     */
    public static void downloadMulti(final Object tag, final List<String> urlList, final List<String> localPathList, final FileCallback<MultiRequestResult> multiFileCallback) {
        Downloader.getInstance().downloadMulti(tag, urlList, localPathList, multiFileCallback);
    }

    /**
     * 多个视频批量下载
     *
     * @param tag
     * @param urlList
     * @param localPathList
     * @param multiFileCallback
     * @param fileCallback
     */
    public static void downloadMulti(final Object tag, final List<String> urlList, final List<String> localPathList, final FileCallback<MultiRequestResult> multiFileCallback, final FileCallback<RequestResult> fileCallback) {
        Downloader.getInstance().downloadMulti(tag, urlList, localPathList, multiFileCallback, fileCallback);
    }

    /**
     * 获取没有进度的单个下载的Observable
     * onNext回调只一次，就是下载成功回调的
     *
     * @param url
     * @param localPath
     * @return
     */
    public static Observable<RequestResult> downloadObservableNoProgress(final String url, final String localPath) {
        return Downloader.getInstance().downloadObservableNoProgress(url, localPath);
    }

    /**
     * 获取带进度的单个下载的Observable
     * 进度会在onNext回调多次
     *
     * @param url
     * @param localPath
     * @return
     */
    public static Observable<RequestResult> downloadObservable(final String url, final String localPath) {
        return Downloader.getInstance().downloadObservable(url, localPath);
    }

    /**
     * 不带进度的批量下载
     * 在onNext回调一次，成功或者失败都只会调用一次
     *
     * @param urlList
     * @param localPathList
     * @return
     */
    public static Observable<MultiRequestResult> downloadMultiObservableNoProgress(final List<String> urlList, final List<String> localPathList) {
        return Downloader.getInstance().downloadMultiObservableNoProgress(urlList, localPathList);
    }

    /**
     * 不带进度的批量下载
     * 在onNext回调一次，成功或者失败都只会调用一次
     *
     * @param urlList
     * @param localPathList
     * @param fileCallback
     * @return
     */
    public static Observable<MultiRequestResult> downloadMultiObservableNoProgress(final List<String> urlList, final List<String> localPathList, final FileCallback fileCallback) {
        return Downloader.getInstance().downloadMultiObservableNoProgress(urlList, localPathList, fileCallback);
    }

    /**
     * 批量下载
     * 进度会在onNext回调多次,进度，成功，失败都在onNext里面回调
     *
     * @param urlList
     * @param localPathList
     * @param fileCallback
     * @return
     */
    public static Observable<MultiRequestResult> downloadMultiObservable(final List<String> urlList, final List<String> localPathList, final FileCallback fileCallback) {
        return Downloader.getInstance().downloadMultiObservable(urlList, localPathList, fileCallback);
    }

}
